package com.koi.system.controller.login;

import com.koi.common.constants.KoiCacheConstants;
import com.koi.common.constants.KoiConstants;
import com.koi.common.redis.RedisUtil;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author ･ᴗ･
 * @description [三种类型验证码]
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    private final RedisUtil redisUtil;

    /**
     * easy-captcha
     * png类型 验证码 - 6分钟过期
     *
     * @author ･ᴗ･
     */
    @GetMapping("/png")
    public Map<String, String> pngCaptcha() {
        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(100, 30, 5);
        // 获取验证码
        String securityCode = specCaptcha.text();
        // 存放Redis验证码的key
        String codeKey = UUID.randomUUID().toString();
        log.info("png验证码Key：{}，png验证码：{}", codeKey, securityCode);
        // 放入redis中并设置过期时间
        redisUtil.setCacheObject(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, securityCode);
        redisUtil.expire(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, KoiConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        Map<String, String> map = new HashMap<>();
        map.put("codeKey", codeKey);
        map.put("captchaPicture", specCaptcha.toBase64());
        return map;
    }

    /**
     * easy-captcha
     * gif验证码 - 6分钟过期
     *
     * @author ･ᴗ･
     */
    @GetMapping("/gif")
    public Map<String, Object> gifCaptcha() {
        // 三个参数分别为宽、高、位数
        GifCaptcha gifCaptcha = new GifCaptcha(100, 30, 5);
        // 设置类型：字母数字混合
        gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        // 获取验证码
        String securityCode = gifCaptcha.text();
        // 存放Redis验证码的key
        String codeKey = UUID.randomUUID().toString();
        log.info("gif验证码Key：{}，gif验证码：{}", codeKey, securityCode);
        // 放入redis中并设置过期时间
        redisUtil.setCacheObject(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, securityCode);
        redisUtil.expire(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, KoiConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<>();
        map.put("codeKey", codeKey);
        map.put("captchaPicture", gifCaptcha.toBase64());
        return map;
    }

    /**
     * easy-captcha
     * 算术类型验证码[不支持SpringBoot3，需要新增依赖，这里未添加] - 6分钟过期
     *
     * @author ･ᴗ･
     */
    // @SneakyThrows
    // @GetMapping("/arithmetic")
    // public Map<String, Object> arithmeticCaptcha() {
    //     // 算术类型
    //     ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(100, 30);
    //     arithmeticCaptcha.setLen(3);  // 几位数运算，默认是两位
    //     arithmeticCaptcha.getArithmeticString();  // 获取运算的公式：4-9+1=?
    //     // 获取验证码
    //     String securityCode = arithmeticCaptcha.text();// 获取运算的结果：-4
    //     // 存放Redis验证码的key
    //     String codeKey = UUID.randomUUID().toString();
    //     log.info("算术验证码Key：{}，算术验证码：{}", codeKey, securityCode);
    //     // 放入redis中并设置过期时间
    //     redisUtil.setCacheObject(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, securityCode);
    //     redisUtil.expire(KoiCacheConstants.CAPTCHA_CODE_KEY + codeKey, KoiConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
    //     Map<String, Object> map = new HashMap<>();
    //     map.put("codeKey", codeKey);
    //     map.put("captchaPicture", arithmeticCaptcha.toBase64());
    //     return map;
    // }


}
